#include "armdefs.h"
#include "armarc.h"

#include <stdbool.h>
#include <stdlib.h>
#include <stdarg.h>

void ErrorExit(int code,const char *fmt,...)
{
  va_list args;
  va_start(args,fmt);
  /* Log it */
  vfprintf(stderr,fmt,args);
  /* Quit */
  exit(code);
}

static void InitFail(int exitcode, char const *which) {
  ErrorExit(exitcode,"%s interface failed to initialise. Exiting\n",
                            which);
}

ARMul_State *emu_state = NULL;

int main(int argc, char **argv)
{
  char *filename;

  if (argc > 1)
    {
      filename = strdup(argv[1]);
    }
  else
    {
      filename = strdup("ROM310");
    }

  ARMul_EmulateInit();
  emu_state = ARMul_NewState();
  ARMul_Reset(emu_state);
  if (ARMul_MemoryInit(emu_state, MemSize_4M, filename) == false)
    InitFail(1, "Memory");
  if (ARMul_CoProInit(emu_state) == false)
    InitFail(2, "Co-Processor");
  ARMul_Reset(emu_state);

  /* Excecute */
  ARMul_DoProg(emu_state);
  emu_state->Reg[15] -= 8; /* undo the pipeline (bogus?) */

  /* Close and Finalise */
  ARMul_CoProExit(emu_state);
  ARMul_MemoryExit(emu_state);

  free(filename);
  return 0;
}
